//
//  XLCustomTextView.m
//  Current
//
//  Created by 于鹏 on 2017/8/23.
//  Copyright © 2017年 YIVIEW. All rights reserved.
//

#import "XLCustomTextView.h"
#import "XLTextView.h"
//#import <Masonry.h>
#import "Masonry.h"
#import "XLAppMacro.h"
#define RightBtnSize CGSizeMake(25, 25)

@interface XLCustomTextView ()<XLTextViewDelegate>
@property (nonatomic, strong)XLTextView *textView;
@property (nonatomic, strong)UIButton *rightButton;//右侧图标

@end
@implementation XLCustomTextView

- (void)awakeFromNib {
    [super awakeFromNib];
    [self configUI];
}
- (instancetype)init {
    self = [super init];
    if (self) {
        [self configUI];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self addConstraints];
}
- (void)configUI {
    self.layer.cornerRadius = 6;
    self.layer.borderWidth = .5f;
    self.layer.borderColor = kXLBlackColor.CGColor;
    
    [self addSubview:self.textView];
    [self addSubview:self.rightButton];
}

- (void)addConstraints {
//    [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(self).offset(-10);
//        make.centerY.equalTo(self).offset(0);
//        make.size.mas_equalTo(RightBtnSize);
//    }];
    
    [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(0);
        make.centerY.equalTo(self).offset(0);
        make.size.mas_equalTo(CGSizeZero);
    }];
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self).offset(0);
        make.right.equalTo(self.rightButton.mas_left).offset(0);
    }];
}
#pragma mark - Setter
- (void)setPlaceHolder:(NSString *)placeHolder {
    _placeHolder = placeHolder;
    self.textView.placeholder = placeHolder;
}
- (void)setFont:(UIFont *)font {
    _font = font;
    self.textView.font = font;
}
- (void)setText:(NSString *)text {
    _text = text;
    self.textView.text = text;
}

- (void)setBorderColor:(UIColor *)borderColor {
    _borderColor = borderColor;
    self.layer.borderColor = borderColor.CGColor;
}

- (void)setShowButton:(BOOL)showButton {
    _showButton = showButton;
    self.rightButton.hidden = !showButton;
    [self.rightButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-10);
        make.centerY.equalTo(self).offset(0);
        make.size.mas_equalTo(RightBtnSize);
    }];
}
#pragma mark - Getter
- (XLTextView *)textView {
    if (!_textView) {
        _textView = [[XLTextView alloc]init];
        _textView.xl_textViewDelegate = self;
    }
    return _textView;
}
- (UIButton *)rightButton {
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rightButton setImage:[UIImage imageNamed:@"microphone"] forState:UIControlStateNormal];
        _rightButton.hidden = YES;
    }
    return _rightButton;
}
#pragma mark - XLTextViewDelegate
- (void)xl_textView:(XLTextView *)textView updatedText:(NSString *)text {
    if (self.xl_customTextViewDelegate && [self.xl_customTextViewDelegate respondsToSelector:@selector(xl_customTextView:updateText:)]) {
        [self.xl_customTextViewDelegate xl_customTextView:self updateText:text];
    }
}
#pragma mark - Public Method
- (CGFloat)xl_textViewHeight {
    return [self.textView xl_textViewHeight];
}

@end
